package com.lamd.myzhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lamd.myzhxy.pojo.Admin;
import com.lamd.myzhxy.pojo.Teacher;
import com.lamd.myzhxy.service.TeacherService;
import com.lamd.myzhxy.util.MD5;
import com.lamd.myzhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/teacherController")
@Api(tags = "教室管理")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation("模糊查询分页")
    @GetMapping("getTeacher/{pageNo}/{pageSize}")
    public Result getTeacher(@PathVariable("pageNo") Integer pageNo,
                             @PathVariable("pageSize") Integer pageSize,
                             Teacher teacher){
        Page<Teacher> page = new Page<>(pageNo,pageSize);
        IPage<Teacher> adminIPage = teacherService.getTeacherByOpr(page,teacher);
        return Result.ok(adminIPage);
    }

    @ApiOperation("添加或者修改教师信息")
    @PostMapping("saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(@RequestBody  Teacher teacher){
        if(teacher.getId() == null || teacher.getId() == 0){
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }

    @ApiOperation("删除单个或者多个教师信息")
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(@RequestBody List<Integer> dis){
        teacherService.removeByIds(dis);
        return Result.ok();
    }
}
